package com.tianchi.lexiang.background;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.ClickRateResetUtil;

public class ClickRateReset extends Activity implements OnClickListener {

	private ClickRateResetUtil instence = ClickRateResetUtil.getInstece();

	private Button sureBtn;
	private Button cancelBtn;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.click_rate_reset);
		initView();
	}

	private void initView() {
		sureBtn = (Button) findViewById(R.id.sure_btn);
		sureBtn.setOnClickListener(this);
		cancelBtn = (Button) findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.sure_btn:
			ClickRateReset.this.finish();
			progressDialog = ProgressDialog.show(
					BackgroundSetting.mBackgroundSetting, getString(R.string.are_reset),
					getString(R.string.reset), true);
			ResetTask rTask = new ResetTask();
			rTask.execute();
			break;
		case R.id.cancel_btn:
			ClickRateReset.this.finish();
			break;

		default:
			break;
		}
	}

	class ResetTask extends AsyncTask<Integer, Integer, String> {

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Integer... params) {
			if (instence.clickRateReset()) {
				return getString(R.string.reset_success);
			}
			return getString(R.string.reset_failu);

		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			AlertDialog.Builder builder = new Builder(
					BackgroundSetting.mBackgroundSetting);
			builder.setTitle(getString(R.string.tip));
			builder.setMessage(result);
			builder.setPositiveButton(getString(R.string.confirm),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();

		}

	}

}
